The Complete Raspberry Pi Manual Magazine by Store Book
Author:Store, Book
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2021-07-07T00:00:00+00:00
Hangman Game Script
| =========âââ, âââ luggage responsible ambassador circumstance î congratulate frequentâ.split()
î return bank[random.randint(0,len(bank))]
+---+
| | O |
/|\ |
/î\ | |
=========âââ] def main():
î gameî=îHangman(rand _ word())
î while not game.hangman _ over():
game.print _ game _ status()
user _ inputî=îinput(â
Enterîaîletter: â) game.guess(user _ input)
class Hangman:
î def _ _ init _ _ (self,word): self.wordî=îword
self.missed _ lettersî=î[] self.guessed _ lettersî=î[] î game.print _ game _ status()
î if game.hangman _ won():
print (â
Congratulations! You have won!!â) else:
print (â
Sorry, you have lost.â)
print (âThe word wasîâî+îgame.word)
î def guess(self,letter):
if letter in self.word and letter not in self. guessed _ letters:
î self.guessed _ letters.append(letter) elif letter not in self.word and letter not in self.missed _ letters:
î self.missed _ letters.append(letter) else:
return False
return True î def hangman _ over(self):
return self.hangman _ won() or (len(self.missed _ letters) == 6)
î def hangman _ won(self):
if â _ â not in self.hide _ word(): î return True
return False
î def hide _ word(self):
rtnî=îââ
for letter in self.word:
î î if letter not in self.guessed _ letters: î î î rtn += â _ â
else:
î î î rtn += letter
return rtn
def print _ game _ status(self):
print (board[len(self.missed _ letters)]) print (âWord:îâî+îself.hide _ word()) print (âLetters Missed: â,)
for letter in self.missed _ letters: î print (letter,)
print ()
print (âLetters Guessed: â,)
for letter in self.guessed _ letters: î print (letter,)
print ()
def rand _ word():
î bankî=îâability about above absolute accessible accommodation accounting beautiful bookstore
î calculator clever engaged engineer enough handsome refrigerator opposite socks interested
î strawberry backgammon anniversary confused dangerous entertainment exhausted impossible
î overweight temperature vacation scissors
î accommodation appointment decrease development
î earthquake environment brand environment necessary î print (â
Goodbye!
â)
if _ _ name _ _ == â _ _ main _ _ â: main()
QUIT() Since this is the last example in our Python code repository, we thought weâd go out with a bang and feature the hangman gallows being drawn with each incorrect guess of the word. Donât worry if it looks misaligned in the text here, this is merely due to the differences between using the Python IDLE editor and pasting the code into a word processor (which formats things differently).
Thereâs plenty you can do to improve, enhance and expand on what weâve presented here. You can include a routine that returns an error if the user enters a number or character. You can include extra points for someone who guesses the entire word in one go rather than one letter at a time and you could perhaps add Chopinâs Funeral March should you lose the game; or something celebratory if you win.
Consider replacing the bank of words too. Theyâre found under the bank list, and could easily be swapped out for something more difficult. If you download www.github.com/ dwyl/english-words you can find a text document with over 466,000 words. Perhaps you could swap the words in the bank to instead read the contents of the text file:
def rand _ word(): î with open(â/home/pi/Downloads/words.txtâ, ârtâ) as f: bank=f.readlines()
î return bank[random.randint(0,len(bank))]
www.bdmpublications.com 93
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12703)
Hello! Python by Anthony Briggs(10009)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9876)
The Mikado Method by Ola Ellnestam Daniel Brolund(9876)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9821)
Dependency Injection in .NET by Mark Seemann(9421)
Hit Refresh by Satya Nadella(8871)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8387)
The Kubernetes Operator Framework Book by Michael Dame(8007)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7841)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7808)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7797)
Grails in Action by Glen Smith Peter Ledbrook(7772)
Practical Computer Architecture with Python and ARM by Alan Clements(7743)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7709)
Robo-Advisor with Python by Aki Ranin(7692)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7656)
Building Low Latency Applications with C++ by Sourav Ghosh(7590)
Svelte with Test-Driven Development by Daniel Irvine(7574)
